package cool.tianyou.blue.bird.receiver;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.annotation.RequiresApi;

import cool.tianyou.blue.bird.util.Util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class PhoneCallReceiver extends BroadcastReceiver {
    private static final String TAG = "PhoneCallReceiver ";
    private OnPhoneCallListener onPhoneCallListener;
    private Context context;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
        int state = tm.getCallState();
        Log.e(TAG, "state = " + state);
        String text = "手机响铃";
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            text = "手机响铃";
        } else if (state == TelephonyManager.CALL_STATE_IDLE) {
            text = "电话挂断";
        }
        HashMap<String, Object> data = new HashMap<>();
        data.put("address", "温馨提示");
        data.put("content", text);
        data.put("time", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
    }

    public void setOnOnPhoneCallListener(OnPhoneCallListener onPhoneCallListener) {
        this.onPhoneCallListener = onPhoneCallListener;
    }

    public interface OnPhoneCallListener {
        void hasNewCall(boolean valid);
    }
}
